package cn.jimmie.learn.viewArt.weight

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View

/**
 * function :
 *
 * <p> Create Time:  2019年01月09日 10:01 </p>
 * <p> @author Jimmie.Qian  </p>
 */

open class BaseView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    val dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, resources.displayMetrics)
    val sp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1f, resources.displayMetrics)
    var h: Int
    val w: Int

    init {
        val attrsArray = intArrayOf(android.R.attr.layout_width, android.R.attr.layout_height)
        val sta = context.obtainStyledAttributes(attrs, attrsArray)
        w = try {
            sta.getDimension(0, 0f).toInt()
        } catch (e: Exception) {
            context.resources.displayMetrics.widthPixels
        }
        @SuppressLint("ResourceType")
        h = try {
            sta.getDimension(1, w.toFloat()).toInt()
        } catch (e: Exception) {
            w
        }
        sta.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(
            MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY)
        )
    }
}